package utils;

import java.sql.*;

public class JDBCUtil {

    static {
        try {
            //注册驱动
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static String getPath() {
        String path = JDBCUtil.class.getResource("/").getPath();
        path = path.substring(1, path.indexOf("classes"));
        return "jdbc:sqlite://" + path + "SIMS.db";
    }

    //获取连接
    public static Connection getConnection() {
        try {
            String path = JDBCUtil.class.getResource("/").getPath();
            path = path.substring(1, path.indexOf("classes"));
            return DriverManager.getConnection("jdbc:sqlite://" + path + "SIMS.db");
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

    //关闭资源
    public static void closeAll(ResultSet rs, Statement st, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
